package com.lianzhihui.romeex.utils.fingersafe;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.ui.base.BaseDialog;
import com.lianzhihui.romeex.utils.LogUtil;

import butterknife.BindView;


/**
 * Created by gaoyang on 2018/06/19.
 */
public class BiometricPromptDialog extends BaseDialog {

    public static final int STATE_NORMAL = 1;
    public static final int STATE_FAILED = 2;
    public static final int STATE_ERROR = 3;
    public static final int STATE_SUCCEED = 4;
    @BindView(R.id.iv_finger)
    ImageView ivFinger;
    @BindView(R.id.tv_finger_title)
    TextView tvFingerTitle;
    @BindView(R.id.cancel_btn)
    TextView cancelBtn;
    @BindView(R.id.root_view)
    RelativeLayout rootView;
    @BindView(R.id.failed_layout)
    RelativeLayout failedLayout;
    @BindView(R.id.show_finger_layout)
    LinearLayout showFingerLayout;
    @BindView(R.id.tv_failed_hint)
    TextView tvFailedHint;
    @BindView(R.id.tv_failed_content)
    TextView tvFailedContent;
    @BindView(R.id.tv_failed_btn)
    TextView tvFailedConfirm;
    private boolean isSet;
    private OnBiometricPromptDialogActionCallback mDialogActionCallback;

    public BiometricPromptDialog(@NonNull Context context, boolean isSet) {
        super(context);
        this.isSet = isSet;
    }

    public BiometricPromptDialog(Context context, int style) {
        super(context, style);
    }

    @Override
    protected int provideViewId() {
        return R.layout.layout_biometric_prompt_dialog;
    }

    @Override
    protected void initView() {
        cancelBtn.setOnClickListener(view -> {
            if (mDialogActionCallback != null) {
                mDialogActionCallback.onCancel();
            }
            dismiss();
        });
        tvFailedConfirm.setOnClickListener(view -> {
            if (mDialogActionCallback != null) {
                mDialogActionCallback.onCancel();
            }
            dismiss();
        });
    }

    public interface OnBiometricPromptDialogActionCallback {
        void onDialogDismiss();

        void onUsePassword();

        void onCancel();
    }


    public void setOnBiometricPromptDialogActionCallback(OnBiometricPromptDialogActionCallback callback) {
        mDialogActionCallback = callback;
    }


    public void setState(int state) {
        switch (state) {
            case STATE_NORMAL:
                tvFingerTitle.setText(context.getResources().getString(R.string.text_finger_nomal));
                LogUtil.error("xxxx", "STATE_NORMAL");
                break;
            case STATE_FAILED:
                tvFingerTitle.setText(context.getResources().getString(R.string.text_try_again));
                LogUtil.error("xxxx", "STATE_FAILED");
                break;
            case STATE_ERROR:
                LogUtil.error("xxxx", "STATE_ERROR");
                if (isSet) {
                    failedLayout.setVisibility(View.VISIBLE);
                    showFingerLayout.setVisibility(View.GONE);
                } else {
                    dismiss();
                }
                break;
            case STATE_SUCCEED:
                LogUtil.error("xxxx", "STATE_SUCCEED");
                if (isSet) {
                    ToastUtils.showShort(context.getResources().getString(R.string.text_finger_sucess));
                }
                dismiss();
                break;
            default:
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (mDialogActionCallback != null) {
            mDialogActionCallback.onDialogDismiss();
        }
    }


}
